package com.cqrt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cqrt.mapper.RTOutRuleMapper;
import com.cqrt.service.RTOutRuleService;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.hustcad.plm.rpm.model.dto.outrule.TyppmOutRuleDTO;
import com.hustcad.plm.rpm.service.outrule.TyppmOutRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RTOutRuleServiceImpl implements RTOutRuleService {

    @Resource
    private TyppmOutRuleService typpmOutRuleService;

    @Resource
    private RTOutRuleMapper rtOutRuleMapper;

    @Override
    public void checkOutRule(JSONObject jsonObject) {
        String jobId = jsonObject.getString("jobId");
        String var1 = jsonObject.getString("立即提交");
        String var2 = jsonObject.getString("是否必须");
        String var3 = jsonObject.getString("任一提交");
        String ruletypeOid = jsonObject.getString("ruletypeOid");
        String typeId = rtOutRuleMapper.selectOutRuleParentTypeIdByTypeId(ruletypeOid);
        List<TyppmOutRuleDTO> list = this.typpmOutRuleService.queryOutRuleByPage(jobId, null, 1, 9999).getList();
        for (TyppmOutRuleDTO typpmOutRuleDTO : list) {

            if (typpmOutRuleDTO.getRuleDisplayName().contains("任一提交")) {
                String pTypeId = rtOutRuleMapper.selectOutRuleParentTypeIdByTypeId(typpmOutRuleDTO.getRuletypeOid());
                if (pTypeId.equals(typeId)) {
                    String ruleDisplayName = typpmOutRuleDTO.getRuleDisplayName();
                    String[] split = ruleDisplayName.split(";");
                    int i = 0;
                    for (String s : split) {
                        if (StrUtil.isBlank(s)) {
                            continue;
                        }
                        if (s.contains("立即提交")) {
                            if (StrUtil.isBlank(var1)) {
                                i = 999;
                                break;
                            }
                            String[] split1 = s.split(":");
                            if (!split1[1].equals(var1)){
                                i = 999;
                                break;
                            }
                            i++;
                        }
                        if (s.contains("是否必须")) {
                            if (StrUtil.isBlank(var2)) {
                                i = 999;
                                break;
                            }

                            String[] split1 = s.split(":");
                            if (!split1[1].equals(var2)){
                                i = 999;
                                break;
                            }
                            i++;
                        }
                        if (s.contains("任一提交")) {
                            if (StrUtil.isBlank(var3)) {
                                i = 999;
                                break;
                            }
                            String[] split1 = s.split(":");
                            if (!split1[1].equals(var3)){
                                i = 999;
                                break;
                            }
                            i++;
                        }
                    }
                    if (split.length != i) {
                        throw new RuntimeException(
                                "交付物规则与同父类型的交付物规则:" + typpmOutRuleDTO.getName() + "不一致!{"+typpmOutRuleDTO.getRuleDisplayName()+"}");
                    }
                }
            }
        }

    }
}
